package com.scilvet.reportcomposer.node;

import com.scilvet.reportcomposer.account.Amount;
import com.scilvet.reportcomposer.report.Line;
import com.scilvet.reportcomposer.report.ReportLine;
import com.scilvet.reportcomposer.report.ReportStore;

public class LineNode implements Node {
	
	private String reportName;
	private int lineNumber;
	private ReportStore reportStore;
	
	public LineNode(String reportName, int lineNumber, ReportStore reportStore) {
		this.reportName = reportName;
		this.lineNumber = lineNumber;
		this.reportStore = reportStore;
	}

	@Override
	public Amount getValue() {
		
		Line line = reportStore.getLine(reportName, lineNumber);
		
		if (!(line instanceof ReportLine)) {
			return Amount.ZERO;
		}
		
		return ((ReportLine)line).getExpression().getValue();
	}
	
	@Override
	public String toString() {
		return "lineref(\"" + reportName + "\", " + lineNumber + ")";
	}

	public String printValues() {
		return getValue().toString();
	}
}
